//
//  DoubleColorBettingViewController.m
//  WangLottery
//
//  Created by 黄修勇 on 12-4-25.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DoubleColorBettingViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "BettingConViewController.h"

@interface DoubleColorBettingViewController ()

@property (nonatomic,retain)UIView *redBellSelectView;
@property (nonatomic,retain)UIView *blueBellSelectView;
@property (nonatomic)NSInteger currentSelectViewIndex;

-(void)reloadSelectButton;
@end

@implementation DoubleColorBettingViewController

@synthesize selectBellScrollView=_selectBellScrollView;
@synthesize titleLabel=_titleLabel;
@synthesize contentView=_contentView;
@synthesize pageControl=_pageControl;
@synthesize buttonBackView=_buttonBackView;
@synthesize lotteryType=_lotteryType;
@synthesize redBellSelectView=_redBellSelectView;
@synthesize blueBellSelectView=_blueBellSelectView;
@synthesize maxSelectRedNumber=_maxSelectRedNumber;
@synthesize maxSelectBlueNumber=_maxSelectBlueNumber;
@synthesize minSelectRedNumber=_minSelectRedNumber;
@synthesize minSelectBlueNumber=_minSelectBlueNumber;
@synthesize titleString=_titleString;
@synthesize firstSelectViewTitle=_firstSelectViewTitle;
@synthesize secoundSelectViewTitle=_secoundSelectViewTitle;
@synthesize selectTitleBtn=_selectTitleBtn;
@synthesize currentSelectViewIndex=_currentSelectViewIndex;

-(void)dealloc{

    [_selectBellScrollView release];
    [_titleLabel release];
    [_contentView release];
    [_pageControl release];
    [_buttonBackView release];
    [_redBellSelectView release];
    [_blueBellSelectView release];
    [_titleString release];
    [_firstSelectViewTitle release];
    [_secoundSelectViewTitle release];
    [_selectTitleBtn release];
    [buleBellArray release];
    [redBellArray release];
    [bellButtonArray release];
    [super dealloc];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.hidesBottomBarWhenPushed=YES;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    buleBellArray=[NSMutableArray new];
    redBellArray=[NSMutableArray new];
    bellButtonArray=[NSMutableArray new];
    
    self.selectBellScrollView.layer.cornerRadius=8;
    self.selectBellScrollView.layer.backgroundColor=[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.4f].CGColor;
    
    self.contentView.layer.cornerRadius=8;
    self.contentView.layer.backgroundColor=[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.4f].CGColor;
    self.contentView.contentSize=CGSizeMake(self.contentView.frame.size.width*2, self.contentView.frame.size.height);
    
    if (_blueBellSelectView&&!_blueBellSelectView.superview) {
        _blueBellSelectView.frame=CGRectMake(_contentView.frame.size.width, 0.0f, _contentView.frame.size.width, _contentView.frame.size.height);
        [_contentView addSubview:_blueBellSelectView];
    }
    
    if (_redBellSelectView&&!_redBellSelectView.superview) {
        _redBellSelectView.frame=CGRectMake(0.0f, 0.0f, _contentView.frame.size.width, _contentView.frame.size.height);
        [_contentView addSubview:_redBellSelectView];
    }
    
    self.buttonBackView.layer.cornerRadius=8;
    self.buttonBackView.layer.backgroundColor=[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.4f].CGColor;
    
    _selectTitleBtn.titleEdgeInsets=UIEdgeInsetsMake(0.0f, -5.0f, 0.0f, 0.0f);
}

-(void)setRedBellSelectView:(UIView *)redBellSelectView{

    if ([redBellSelectView isEqual:_redBellSelectView]) {
        return;
    }
    [_redBellSelectView removeFromSuperview];
    [_redBellSelectView release];
    _redBellSelectView=[redBellSelectView retain];
    
    if (_contentView) {
        _redBellSelectView.frame=CGRectMake(0.0f, 0.0f, _contentView.frame.size.width, _contentView.frame.size.height);
        [_contentView addSubview:_redBellSelectView];
    }
}

-(void)setBlueBellSelectView:(UIView *)blueBellSelectView{

    if ([blueBellSelectView isEqual:_blueBellSelectView]) {
        return;
    }
    [_blueBellSelectView removeFromSuperview];
    [_blueBellSelectView release];
    _blueBellSelectView=[blueBellSelectView retain];
    
    if (_contentView) {
        _blueBellSelectView.frame=CGRectMake(_contentView.frame.size.width, 0.0f, _contentView.frame.size.width, _contentView.frame.size.height);
        [_contentView addSubview:_blueBellSelectView];
    }
}

-(void)setLotteryType:(LotteryType)lotteryType{

    _maxSelectRedNumber=16;
    
    _maxSelectBlueNumber=16;
    
    if (lotteryType==LotteryTypeShuangSeQiu||lotteryType==LotteryTypeDaLeTou) {
        
        if (lotteryType==_lotteryType) {
            return;
        }
        _lotteryType=lotteryType;
        
        NSInteger maxRedNum=0;
        NSInteger maxBluenum=0;
        
        
        if (_lotteryType==LotteryTypeShuangSeQiu) {
            _minSelectRedNumber=6;
            _minSelectBlueNumber=1;
            
            self.titleString=@"红球至少选6个号,蓝球至少选1个号";
            
            self.firstSelectViewTitle=@"蓝球";
            self.secoundSelectViewTitle=@"红球";
            maxRedNum=33;
            maxBluenum=16;
        }else {
            _minSelectRedNumber=5;
            _minSelectBlueNumber=2;
            self.titleString=@"前区至少选5个号,后区至少选2个号";
            self.firstSelectViewTitle=@"后区";
            self.secoundSelectViewTitle=@"前区";
            maxRedNum=35;
            maxBluenum=12;
        }
        
        BellContentView *tempRedContentView=[[BellContentView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f)];
        tempRedContentView.backgroundColor=[UIColor clearColor];
        tempRedContentView.numbersOfLine=7;
        for (int i=1; i<=maxRedNum; i++) {
            BellButton *btn=[BellButton creatBellWithFrame:CGRectMake(0.0f, 0.0f, 30.0f, 30.0f)];
            btn.tag=i;
            btn.bellColor=BellColorRed;
            btn.number=i;
            
            [tempRedContentView addSubview:btn];
            [btn addTarget:self action:@selector(bellDone:) forControlEvents:UIControlEventTouchUpInside];
        }
        self.redBellSelectView=tempRedContentView;
        [tempRedContentView release];
        
        BellContentView *tempBlueContentView=[[BellContentView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f)];
        for (int i=1; i<=maxBluenum; i++) {
            BellButton *btn=[BellButton creatBellWithFrame:CGRectMake(0.0f, 0.0f, 30.0f, 30.0f)];
            btn.tag=i;
            btn.number=i;
            btn.bellColor=BellColorBlue;
            [tempBlueContentView addSubview:btn];
            [btn addTarget:self action:@selector(bellDone:) forControlEvents:UIControlEventTouchUpInside];
        }
        tempBlueContentView.numbersOfLine=7;
        self.blueBellSelectView=tempBlueContentView;
        [tempBlueContentView release];
        
        self.titleLabel.text=_titleString;
    }
}




-(void)setFirstSelectViewTitle:(NSString *)firstSelectViewTitle{

    if ([firstSelectViewTitle isEqualToString:_firstSelectViewTitle]) {
        return;
    }
    [_firstSelectViewTitle release];
    _firstSelectViewTitle=[firstSelectViewTitle retain];
        
    if (_pageControl.currentPage==0) {
        [_selectTitleBtn setTitle:_firstSelectViewTitle forState:UIControlStateNormal];
    }
}

-(void)setSecoundSelectViewTitle:(NSString *)secoundSelectViewTitle{

    if ([secoundSelectViewTitle isEqualToString:_secoundSelectViewTitle]) {
        return;
    }
    [_secoundSelectViewTitle release];
    _secoundSelectViewTitle=[secoundSelectViewTitle retain];
    
    if (_pageControl.currentPage==1) {
        [_selectTitleBtn setTitle:_secoundSelectViewTitle forState:UIControlStateNormal];
    }
}

-(void)setCurrentSelectViewIndex:(NSInteger)currentSelectViewIndex{

    if (_currentSelectViewIndex==currentSelectViewIndex) {
        return;
    }
    _currentSelectViewIndex=currentSelectViewIndex;
    
    if (_currentSelectViewIndex==0) {
        _pageControl.currentPage=0;
        [_selectTitleBtn setTitle:_firstSelectViewTitle forState:UIControlStateNormal];
        [_selectTitleBtn setBackgroundImage:[UIImage imageNamed:@"blue_arrow.png"] forState:UIControlStateNormal];
        _selectTitleBtn.titleEdgeInsets=UIEdgeInsetsMake(0.0f, -5.0f, 0.0f, 0.0f);
        if (!_contentView.isDragging) {
            [_contentView scrollRectToVisible:CGRectMake(0.0f, 0.0f, _selectBellScrollView.frame.size.width, _selectBellScrollView.frame.size.height) animated:YES];
        }
    }else {
        _pageControl.currentPage=1;
        [_selectTitleBtn setTitle:_secoundSelectViewTitle forState:UIControlStateNormal];
        [_selectTitleBtn setBackgroundImage:[UIImage imageNamed:@"red_arrow.png"] forState:UIControlStateNormal];
        _selectTitleBtn.titleEdgeInsets=UIEdgeInsetsMake(0.0f, 5.0f, 0.0f, 0.0f);
        if (!_contentView.isDragging) {
            [_contentView scrollRectToVisible:CGRectMake(_selectBellScrollView.frame.size.width, 0.0f, _selectBellScrollView.frame.size.width, _selectBellScrollView.frame.size.height) animated:YES];
        }
    }
}

-(IBAction)pageDone:(id)sender{

    self.currentSelectViewIndex=_pageControl.currentPage;
}

-(IBAction)selectBtnDone:(id)sender{

    if (_currentSelectViewIndex==0) {
        self.currentSelectViewIndex=1;
    }else {
        self.currentSelectViewIndex=0;
    }
}

-(IBAction)randomSelect:(id)sender{

    [self removeAllSelect];

    NSInteger typeRedSelectNum=0;
    NSInteger typeBlueSelectNum=0;
    
    NSInteger maxRedNum=0;
    NSInteger maxBluenum=0;
    
    if (_lotteryType==LotteryTypeShuangSeQiu) {
        
        typeRedSelectNum=6;
        typeBlueSelectNum=1;
        
        maxRedNum=33;
        maxBluenum=16;
    }
    else if(_lotteryType==LotteryTypeDaLeTou){
        typeRedSelectNum=5;
        typeBlueSelectNum=2;
        
        maxRedNum=35;
        maxBluenum=12;
        
    }
    
     NSMutableArray *redRandomBall=[[NSMutableArray alloc] init];
    
    //机选 随机红球
    for (int i=0; i<typeRedSelectNum; i++) {
        
        int index = arc4random() % maxRedNum + 1;
        
        BOOL mark=YES; // 查找是否生成相同的标记
        for (int j=0; j<[redRandomBall count]; j++) {
            
            int index2=[[redRandomBall objectAtIndex:j] intValue];
            
            if (index2 == index) {
                mark=NO;
                i--;
                break;
            }
        }
        if (mark) [redRandomBall addObject:[NSNumber numberWithInt:index]];

    }
    
    for (int  r=0; r<[redRandomBall count]; r++) {
        
        int index=[[redRandomBall objectAtIndex:r] intValue];
        
        BellButton *btn=(BellButton *)[self.redBellSelectView viewWithTag:index];
        
        btn.bellState=BellStateSelct;
        
        [self addSelectBellByNumber:index isRed:YES];

    }
    
    [redRandomBall removeAllObjects];
    [redRandomBall release];
    
    // 随机生成蓝球
    NSMutableArray *blueRandomBall=[[NSMutableArray alloc] init];
    
    for (int i=0; i<typeBlueSelectNum; i++) {
        
        int index = arc4random() % maxBluenum + 1;
        
        BOOL mark=YES; // 查找是否生成相同的标记
        for (int j=0; j<[blueRandomBall count]; j++) {
            
            int index2=[[blueRandomBall objectAtIndex:j] intValue];
            
            if (index2 == index) {
                mark=NO;
                i--;
                break;
            }
        }
        if (mark) [blueRandomBall addObject:[NSNumber numberWithInt:index]];
        
    }

    for (int  r=0; r<[blueRandomBall count]; r++) {
        
        int index=[[blueRandomBall objectAtIndex:r] intValue];
        
        BellButton *btn=(BellButton *)[self.blueBellSelectView viewWithTag:index];
        
        btn.bellState=BellStateSelct;
        
        [self addSelectBellByNumber:index isRed:NO];
        
    }
    
    [blueRandomBall removeAllObjects];
    [blueRandomBall release];
    
    [self reloadSelectButton];
    
}

-(IBAction)betting:(id)sender{

    if ([self checkSelectIsOk]==NO) {
        
        [self showAlertViewWithTitle:@"信息提示" msg:_titleString];
        return;
    }
    
    BettingConViewController *bettingConViewController=[[BettingConViewController alloc]initWithNibName:@"BettingConViewController" bundle:nil];
    [self.navigationController pushViewController:bettingConViewController animated:YES];
    [bettingConViewController release];
}

-(IBAction)doColletion:(id)sender{

    if ([self checkSelectIsOk]==NO) {
        
        [self showAlertViewWithTitle:@"信息提示" msg:_titleString];
        return;
    }
}

-(void)bellDone:(BellButton *)bellBtn{

    if (bellBtn.bellState==BellStateNormal) {
        if (bellBtn.bellColor==BellColorRed) {
            [self addSelectBellByNumber:bellBtn.tag isRed:YES];
        }else {
            [self addSelectBellByNumber:bellBtn.tag isRed:NO];
        }
        bellBtn.bellState=BellStateSelct;
    }else {
        if (bellBtn.bellColor==BellColorRed) {
            [self removeSelectBellByNumber:bellBtn.tag isRed:YES];
        }else {
            [self removeSelectBellByNumber:bellBtn.tag isRed:NO];
        }
        bellBtn.bellState=BellStateNormal;
    }
    [self reloadSelectButton];
}

-(BOOL)addSelectBellByNumber:(NSInteger)number isRed:(BOOL)isRed{

    if (isRed) {
        [redBellArray addObject:[NSNumber numberWithInt:number]];
    }else {
        [buleBellArray addObject:[NSNumber numberWithInt:number]];
    }
    BellButton *btn=[BellButton creatBellWithFrame:CGRectMake(BellLeftMargin+bellButtonArray.count*(40.0f+10.0f), 54.0f/2-40.0f/2, 40.0f, 40.f)];
    [self.selectBellScrollView addSubview:btn];
    [bellButtonArray addObject:btn];
    
    return YES;
}

-(BOOL)removeSelectBellByNumber:(NSInteger)number isRed:(BOOL)isRed{

    if (isRed) {
        [redBellArray removeObject:[NSNumber numberWithInt:number]];
    }else {
        [buleBellArray removeObject:[NSNumber numberWithInt:number]];
    }
    [[bellButtonArray lastObject]removeFromSuperview];
    [bellButtonArray removeLastObject];
    [self reloadSelectButton];
    return YES;
}

-(void)removeAllSelect{

    for (UIView *view in bellButtonArray) {
        [view removeFromSuperview];
    }
    
    for (NSNumber *num in redBellArray) {
        NSInteger tag=[num intValue];
        BellButton *btn=(BellButton *)[_redBellSelectView viewWithTag:tag];
        [btn setBellState:BellStateNormal];
    }
    
    for (NSNumber *num in buleBellArray) {
        NSInteger tag=[num intValue];
        BellButton *btn=(BellButton *)[_blueBellSelectView viewWithTag:tag];
        [btn setBellState:BellStateNormal];
    }
    
    [redBellArray removeAllObjects];
    [buleBellArray removeAllObjects];
    [bellButtonArray removeAllObjects];
    
    _selectBellScrollView.contentSize=CGSizeMake(_selectBellScrollView.frame.size.width, _selectBellScrollView.frame.size.height);
    
}

-(BOOL)checkSelectIsOk{

    if (redBellArray.count<_minSelectRedNumber) {
        return NO;
    }
    if (buleBellArray.count<_minSelectBlueNumber) {
        return NO;
    }
    return YES;
}

-(void)reloadSelectButton{

    UIView *view=[bellButtonArray lastObject];
    float width=0.0f;
    if (view) {
        width=view.frame.origin.x+view.frame.size.width+BellLeftMargin;
    }else {
        width=self.selectBellScrollView.frame.size.width;
    }
    self.selectBellScrollView.contentSize=CGSizeMake(width,_selectBellScrollView.frame.size.height);
    [self.selectBellScrollView scrollRectToVisible:CGRectMake(_selectBellScrollView.contentSize.width-_selectBellScrollView.frame.size.width, 0.0f, _selectBellScrollView.frame.size.width, _selectBellScrollView.frame.size.height) animated:YES];
    [redBellArray sortUsingSelector:@selector(compare:)];
    [buleBellArray sortUsingSelector:@selector(compare:)];
    
    for (int i=0; i<redBellArray.count; i++) {
        BellButton *btn=[bellButtonArray objectAtIndex:i];
        btn.bellState=BellStateSelct;
        btn.bellColor=BellColorRed;
        btn.number=[[redBellArray objectAtIndex:i] intValue];
    }
    
    for (int i=0; i<buleBellArray.count; i++) {
        BellButton *btn=[bellButtonArray objectAtIndex:redBellArray.count+i];
        btn.bellColor=BellColorBlue;
        btn.bellState=BellStateSelct;
        btn.number=[[buleBellArray objectAtIndex:i] intValue];
    }
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark-UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    self.currentSelectViewIndex=scrollView.contentOffset.x/scrollView.frame.size.width;

}
@end
